<?php
	//文章管理列表
	class ContentController extends Controller{
		private $db;
		public function __init(){
			$this->db=K('Content');//实例化一个对象
		}
		//文章列表
		public function index(){
			$data=$this->db->getList();
			//分配文章数据
			$this->assign('data',$data['data']);
			//分配页数
			$this->assign('page',$data['page']);
			$this->display();
		}
		//添加文章
		public function add(){
			if(IS_POST){
				if($this->db->addarticle()){
					$this->success('添加文章成功','index');
				}else{
					$this->error($this->db->error);
				}
			}else{
				$category=K('Category');
				$data=$category->all();
				//分配栏目
				$this->assign('data',$data);
				$this->display();
			}
		}
		//编辑文章
		public function edit(){
			if(IS_POST){
				if($this->db->editarticle()){
					$this->success('修改文章成功','index');
				}else{
					$this->error($this->db->error);
				}
			}else{
				$aid=Q('aid',0,'intval');
				$cid=Q('cid',0,'intval');
				$data=$this->db->where("aid=$aid")->find();
				//缩略图处理(为了编辑时正常显示)
				$data['thumb']=array(
					array('alt'=>'','path'=>$data['thumb'])
				);
				//所有的栏目数据
				$category=K('Category');
				$allcategory=$category->all();
				foreach ($allcategory as $id => $cat) {
					//给个selected属性
					$allcategory[$id]['selected']=$cat['cid']==$cid?"selected=''":'';
				}
				//分配栏目
				$this->assign('allcategory',$allcategory);
				//分配文章数据
				$this->assign('data',$data);
				$this->display();
			}
		}
		//删除文章
		public function delete(){
			$aid=Q('aid',0,'intval');
			$data=$this->db->find($aid);
			// p($data);exit;
			//删除缩略图
			is_file($data['thumb']) and unlink($data['thumb']);
			if($this->db->where("aid=$aid")->deletearticle()){
				$this->success('删除文章成功','index');
			}else{
				$this->error($this->db->error);
			}
		}

	}




?>